import androidhelper
import time


def write_to_file(text):
    f = open('command.txt', mode='w')
    f.write(text)
    f.flush()
    f.close()


def pre():
    write_to_file('pre')


def pause():
    write_to_file('pause')


def stop():
    write_to_file('stop')


def unpause():
    write_to_file('unpause')


def next():
    write_to_file('next')


def exit():
    write_to_file('exit')


def main():
    # droid = androidhelper.Android()
    
    title = '操作'
    items = ['上一章', '暂停', '停止', '恢复', '下一章', '关闭']
    f_m = {'0': pre, '1': pause, '2': stop, '3': unpause, '4':next, '5':exit}
    
    tips = '请输入数字选择操作(使用time-3-1 模式的命令可定时控制):\n'
    for i in range(0, len(items)):
        tips += str(i) + ':' + items[i] + '\n'
    
    while True:
        # droid.dialogCreateAlert(title)
        # droid.dialogSetItems(items)
        # droid.dialogShow()
        # response = droid.dialogGetResponse().result
        # if 'canceled' in response and response['canceled']:
        #     continue
        #print(response)
        # print(response, items[response['item']])
        #f_m[response['item']]()
        
        c = input(tips)
        if c.startswith('time-'):
            sp = c.split('-')
            if len(sp) == 3:
                c = sp[2]
                time.sleep(int(sp[1]))
        if c in f_m:
            f_m[c]()


if __name__ == "__main__":
    main()
